﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace GameStarter
{

    public partial class MainForm : Form
    {
        private const string COPYRIGHT = "Copyright © 2024 CloudC_Zh.\nAll right reserved.";
        private string xmlpath= ".\\config.xml";
        private string pw;
        private int maxlength;
        private string exePth;
        public MainForm()
        {
            InitializeComponent();
            this.StartPosition=FormStartPosition.CenterScreen;
            
            labelbonus.Text = "";
            getDataFromXml();
            textpw.MaxLength = maxlength;
            pw = genPW(maxlength);
            genDate();
            labelinfo.Text = COPYRIGHT;
        }

        private void getDataFromXml()
        {
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(xmlpath);
                XmlNode root = xmlDocument.DocumentElement;
                maxlength = int.Parse(root.SelectSingleNode("maxlength").InnerText);
                exePth = root.SelectSingleNode("exepath").InnerText;
                if (!File.Exists(exePth))
                {
                    MessageBox.Show("文件" + exePth + "不存在！", "提示");
                    this.Close();
                }
            }catch (Exception ex)
            {
                MessageBox.Show("读取xml出现错误"+ex, "提示");
                this.Close();
            }
            
        }


        /// <summary>
        /// 日期逻辑
        /// </summary>
        /// <returns></returns>
        private void genDate()
        {
            DateTime dt =DateTime.Now;
            if(dt.DayOfWeek == DayOfWeek.Saturday)
            {
                labelbonus.Text = "今天周六!\n密码是" + pw;
            }
            else
            {
                int res = 0;
                string bb = "";
                for(int i = 0; i < maxlength; i++)
                {
                    int t= int.Parse(pw[i].ToString());
                    res += t;
                    if (t == 0) bb += "第" + (i+1) + "位数是0\n";
                    else if(t%2==1) bb += "第" + (i + 1) + "位数是奇数\n";
                    else if(t % 2 == 0) bb += "第" + (i + 1) + "位数是偶数\n";
                }
                labelbonus.Text = "密码提示：\n"+maxlength+"位数之和=" + res+"\n"+bb;
            }
            labeldate.Text = new StringBuilder().Append(dt.Year).Append("年").Append(dt.Month).Append("月").Append(dt.Day).Append("日").ToString();
        }

        /// <summary>
        /// 生成密码
        /// </summary>
        /// <param name="maxlength"></param>
        /// <returns></returns>
        private string genPW(int maxlength)
        {
            Random random = new Random();
            int t=random.Next(1, (int)Math.Pow(10,maxlength));
            StringBuilder sb = new StringBuilder();
            sb.Append(t.ToString());
            int tt=maxlength-t.ToString().Length;
            
            while (tt > 0)
            {
                sb.Insert(0, "0");
                tt--;
            }
            
            return sb.ToString();
        }

        

        private void buttonsure_Click(object sender, EventArgs e)
        {
            if (!textpw.Text.Equals(pw))
            {
                MessageBox.Show("密码错误!", "提示");
                textpw.Text = "";
                
            }
            else
            {
                try {
                    Process.Start(exePth);
                }catch(Exception excption) {
                }
                Environment.Exit(0);
            }
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void labeldate_Click(object sender, EventArgs e)
        {

        }

        private void labelinfo_Click(object sender, EventArgs e)
        {

        }

        private void labelbonus_Click(object sender, EventArgs e)
        {

        }

        private void buttonrefrash_Click(object sender, EventArgs e)
        {
            pw=genPW(maxlength);
            genDate();
        }
    }
}
